/**
 * Created by zhangsong on 2019-06-03.
 */
import URI from '@/services/uri';
import {
  requestPostByToken,
  requestGetByToken,
  requestPutByToken,
  requestDeleteByToken,
} from '@/utils/request';

const url = URI.application;

export const listByParam = async query => {
  const {
    fullName,
    make,
    vehicleNumber,
    category,
    colour,
    expiryDate,
    staffNo,
    // TODO: API 缺少部门字段
    department,
    telNo,
    date,
    pageSize,
    page,
    ...other
  } = query;

  const result = await requestGetByToken({
    url: url.vehiclesUrl.list,
    query: {
      ...other,
      page_size: pageSize,
      page_num: page,
      make,
      category,
      colour,
      full_name_of_the_registered_owner: fullName,
      vehicle_prefix: '',
      vehicle_number: vehicleNumber,
      pass_expiry_date: expiryDate,
      staff_no: staffNo,
      telephone: telNo,
    },
  });
  const rows = result.data.map(item => ({
    id: item.id,
    make: item.mark,
    category: item.category,
    colour: item.category,
    fullName: item.full_name_of_the_registered_owner,
    vehicleNumber: item.vehicle_number,
    expiryDate: item.pass_expiry_date,
    staffNo: item.staff_no,
    telNo: item.telephone,
    fileGroupId: item.file_group_id,
  }));
  return {
    data: rows,
    page: result.page,
    pageSize: result.page_size,
    total: result.result,
  };
};

export const getById = async id => {
  const data = await requestGetByToken({
    url: url.vehiclesUrl.getById.replace('{id}', id),
  });
  return {
    id: data.id,
    mark: data.mark,
    category: data.category,
    colour: data.colour,
    fullName: data.full_name_of_the_registered_owner,
    vehicleNumber: data.vehicle_number,
    expiryDate: data.pass_expiry_date,
    staffNo: data.staff_no,
    telNo: data.telephone,
    fileGroupId: data.file_group_id,
  };
};

export const saveContractor = data => {
  const {
    fullName,
    make,
    vehicleNumber,
    category,
    colour,
    expiryDate,
    staffNo,
    // TODO: API 缺少部门字段
    department,
    telNo,
  } = data;
  const param = {
    mark: make,
    category,
    colour,
    full_name_of_the_registered_owner: fullName,
    vehicle_number: vehicleNumber,
    pass_expiry_date: expiryDate,
    staff_no: staffNo,
    telephone: telNo,
    vehicle_prefix: '',
    vehicle_reg_no: '',
  };
  return requestPostByToken({
    url: url.vehiclesUrl.save,
    body: param,
  });
};

export const removeById = id =>
  requestDeleteByToken({
    url: url.vehiclesUrl.removeById.replace('{id}', id),
  });

export const updateById = ({ id, body }) => {
  const {
    fullName,
    make,
    vehicleNumber,
    category,
    colour,
    expiryDate,
    staffNo,
    // TODO: API 缺少部门字段
    department,
    telNo,
  } = body;
  const param = {
    mark: make,
    category,
    colour,
    full_name_of_the_registered_owner: fullName,
    vehicle_prefix: '',
    vehicle_number: vehicleNumber,
    pass_expiry_date: expiryDate,
    staff_no: staffNo,
    telephone: telNo,
  };
  return requestPutByToken({
    url: url.vehiclesUrl.modifyById.replace('{id}', id),
    body: param,
  });
};
